package com.bolt.admin.module.test.service;

import com.bolt.admin.module.dict.entity.DictDetailEntity;
import com.bolt.admin.module.test.dto.FruitOrderDTO;
import com.bolt.admin.module.test.entity.FruitOrderEntity;
import com.bolt.admin.module.test.repository.FruitOrderRepository;
import com.bolt.support.base.service.BaseService;
import com.bolt.support.dynamic.datasource.annotation.DataSourceName;
import com.bolt.support.query.nativequery.parall.ParallelQuery;
import com.bolt.support.query.nativequery.parall.ParallelQueryResult;
import com.bolt.support.spring.jpa.jpql.Clauses;
import com.bolt.support.spring.jpa.repository.BasicJpaRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Auto Generate Javadoc
 *
 * @author 日期:2021-06-01 15:18:29
 *         查询汇总演示-水果订单表
 **/
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
@RequiredArgsConstructor
public class FruitOrderService extends BaseService<FruitOrderEntity, Integer> {

    private final FruitOrderRepository fruitOrderRepository;

    @Override
    protected BasicJpaRepository<FruitOrderEntity, Integer> getRepository() {
        return fruitOrderRepository;
    }


    public List<FruitOrderDTO> test() {
        List<FruitOrderDTO> data = this.jpaQueryFactory.select(FruitOrderDTO.class,
                Clauses.of(FruitOrderEntity::getFruitName),
                Clauses.of(FruitOrderEntity::getOrderMonth),
                Clauses.of(FruitOrderEntity::getSaleCount),
                Clauses.of(FruitOrderEntity::getSalePrice),
                Clauses.of(FruitOrderEntity::getTotalAmt))
                .from(Clauses.of(FruitOrderEntity.class))
                .unpivot("saleCount:销售数量,salePrice:销售单价,totalAmt:总金额", "indexName,indexValue")
                .fetch();
        return data;
    }

    public List<FruitOrderDTO> test2() {
        return this.nativeQueryList("qstart_unpivot_case", null, FruitOrderDTO.class);
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<ParallelQueryResult> test3() {
        return this.parallelNativeQuery(
                Arrays.asList(new ParallelQuery("qstart_unpivot_case", null, FruitOrderDTO.class),
                        new ParallelQuery("qstart_unpivot_case", null, FruitOrderDTO.class),
                        new ParallelQuery("qstart_unpivot_case", null, FruitOrderDTO.class))
                , null
        );
    }
    @DataSourceName("mysql")
    public List test42(Map<String, Object> param) {
        return this.nativeQueryLimit("dit_test_1", param,10, DictDetailEntity.class);
    }

    @DataSourceName("oracle")
    public List test43(Map<String, Object> param) {
        return this.nativeQueryLimit("dit_test_3", param, 10);
    }


}
